<?php
namespace Home\Service;

use Think\Service\BasicService;

class HsGroupOrderService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function AddAll($model) {
        $data = $this->db->addAll($model);
        return $data;
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->where($condition)->find();
    }

    public function query($condition, $limit = array(), $field = '*', $order = '', $join = '') {
        $data = $this->db->join($join)->where($condition)->field($field)->limit($limit['start'], $limit['end'])->order($order)->select();
        return $data;
    }

    public function count($condition) {
        return $this->db->where($condition)->count();
    }

    public function AddGroupOrder($data) {
        $user_id = passport_decrypt($data['token'], C('JM_KEY'));
        // 添加开团表
        $save['CREATE_TIME'] = date('Y-m-d H:i:s');
        $save['END_TIME'] = date('Y-m-d H:i:s', (time() + 2 * 24 * 60 * 60));
        $save['GROUP_USER_ID'] = $user_id;
        $this->db->startTrans();
        $GID = $this->db->add($save);
        if ($GID) {
            //加入开团
            $save1['GROUP_ID'] = $GID;
            $save1['USER_ID'] = $user_id;
            $save1['ORDER_ID'] = $data['order_id'];
            $save1['JOIN_TIME'] = date('Y-m-d H:i:s');
            $this->db = M('group_order_log');
            $OK = $this->db->add($save1);
            if ($OK) {
                $this->db->commit();
                $res['TID'] = $GID;
                $res['OK'] = 1;
                return $res;
            }
        }
        $this->db->rollback();
        $res['TID'] = '';
        $res['OK'] = -1;
        return $res;


    }


}


?>